package com.ddd.mill.infrastructure.persistent.repository.impl.dictionary;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ddd.mill.domain.aggregate.dictionary.entity.Dictionary;
import com.ddd.mill.domain.aggregate.dictionary.repository.DictionaryRepository;
import com.ddd.mill.infrastructure.persistent.dao.dictionary.DictionaryDao;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


import java.util.List;
import java.util.Objects;

/**
 * @author liu
 * @title: DictionaryMyBatisRepositoryImpl
 * @projectName trading-center
 * @date 2020/11/3015:40
 */
@Component
public class DictionaryMyBatisRepositoryImpl implements DictionaryRepository {

    @Resource
    DictionaryDao dao;

    @Override
    public List<Dictionary> unitList() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("label", "unit");
        return dao.selectList(queryWrapper);
    }

    @Override
    public Dictionary load(Long id) {
        return dao.selectById(id);
    }

    @Override
    public Long save(Dictionary entity) {
        if (!Objects.isNull(entity.getId()) && !Objects.isNull(dao.selectById(entity.getId()))) {
            dao.updateById(entity);
        } else {
            dao.insert(entity);
        }
        return entity.getId();
    }
}
